﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;

namespace HuaCheng.DataAccess
{
    class StepHelper
    {
        /// <summary>
        /// 根据设备号，柜号，通道号查测试Id
        /// </summary>
        /// <param name="devId"></param>
        /// <param name="uId"></param>
        /// <param name="cId"></param>
        /// <returns></returns>
        public static string GetTestIdById(string ip,int gid,int mid,int cId)
        {
            string testId = "0";
            string sqlStr = "select distinct test_id from step where dev_ip = @ip and cabinet_id =@gid and mid_id = @mid and chl_id = @cid";
            DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sqlStr);
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ip", ip));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", gid));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", mid));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cid", cId));
            try
            {
                using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            testId = Convert.ToString(reader[0]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);
            }
            return testId;          
        }
        /// <summary>
        /// 获取最大的测试Id
        /// </summary>
        /// <returns></returns>
        public static int GetMaxTestId()
        {
            int testId = 0;
            string sqlStr = "select max(test_id) from step";
            DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sqlStr);
            try
            {
                using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            testId = Convert.ToInt32(reader[0]);
                        }
                    }
                }
            }
            catch (DbException ex)
            {
                 Console.WriteLine(ex.Message + ex.StackTrace);
            }
            //如果表中没有数据
            catch(InvalidCastException e)
            {
                Console.WriteLine(e.Message + e.StackTrace);
                testId = 0;
            }
            return testId;
        }
        /// <summary>
        /// 根据设备号，柜号，通道号删除记录
        /// </summary>
        /// <param name="devId"></param>
        /// <param name="uId"></param>
        /// <param name="cId"></param>
        public static void DeleteStepById(string ip, int gid, int mid, int cId)
        {
            try
            {
                string sqlStr = "delete from step where dev_ip = @ip and cabinet_id=@gid and mid_id = @mid and chl_id = @cid";
                DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sqlStr);
                cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ip", ip));
                cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", gid));
                cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", mid));
                cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cid", cId));
                DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                 Console.WriteLine(ex.Message + ex.StackTrace);
            }
        }
        /// <summary>
        /// 工步表中新增工步记录
        /// </summary>
        /// <param name="bts"></param>
        public static void InsertNewStep(Step step)
        {
            string sql = "insert into step(test_id,dev_ip,cabinet_id,mid_id,chl_id,step_type,step_index,step_name,step_time,step_data,standard_flag,dcir_flag)" +
                " values (@testid,@devip,@gid,@mid,@chlid,@steptype,@stepindex,@stepname,@steptime,@stepdata,@standardflag,@dcirflag)";
            try
            {
                using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
                {
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@testid", step.TestId));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@devip", step.DevIp));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", step.GID));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", step.MID));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@chlid", step.ChlId));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@steptype", step.StepType));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@stepindex", step.StepIndex));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@stepname", step.StepName));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@steptime", step.StepTime));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@stepdata", step.StepData));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@standardflag", step.StandardFlag));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@dcirflag", step.DcirFlag)); 
                    DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); }
        }
        /// <summary>
        /// 根据设备号，柜号，通道号查询byte数组列表
        /// </summary>
        /// <param name="devId"></param>
        /// <param name="uId"></param>
        /// <param name="cId"></param>
        /// <returns></returns>
        public static List<byte[]> GetBinaryListById(string testId,string ip,int gid,int mid,int cId)
        {
            List<byte[]> biList = new List<byte[]>();
            string sqlStr="";
            if (testId == "0")
            {
                sqlStr = "select step_data from step where dev_ip = @ip and cabinet_id=@gid and mid_id= @mid and chl_id = @cid";
            }
            else
            {
                sqlStr = "select step_data from h_step where test_id=@testid and dev_ip = @ip and cabinet_id=@gid and mid_id= @mid and chl_id = @cid";
            }
            DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sqlStr);
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@testid", testId));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ip",ip));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", mid));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", gid));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cid", cId));
            try
            {
                using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            byte[] stepData = new byte[reader.GetBytes(0, 0, null, 0, int.MaxValue)];
                            reader.GetBytes(0, 0, stepData, 0, stepData.Length);
                            biList.Add(stepData);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                 Console.WriteLine(ex.Message + ex.StackTrace);
            }
            return biList;
        }
        /// <summary>
        /// 根据设备号，柜号，通道号查标准或是专业工步
        /// </summary>
        /// <param name="devId"></param>
        /// <param name="uId"></param>
        /// <param name="cId"></param>
        /// <returns></returns>
        public static bool GetStepFlagById(string ip, int gid,int mid, int cId)
        {
            bool stepFlag=false;
            string sqlStr = "select standard_flag from step where dev_ip = @ip and cabinet_id=@gid and mid_id= @mid and chl_id = @cid";
            DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sqlStr);
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ip", ip));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", gid));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", mid));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cid", cId));
            try
            {
                using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            stepFlag = reader.IsDBNull(0) ? true : Convert.ToBoolean(reader["standard_flag"]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);
            }
            return stepFlag;
        }
        /// <summary>
        /// 根据设备号，柜号，通道号查是否为DCIR测试
        /// </summary>
        /// <param name="devId"></param>
        /// <param name="uId"></param>
        /// <param name="cId"></param>
        /// <returns></returns>
        public static bool GetDcirFlagById(string ip, int gid, int mid, int cId)
        {
            bool dcirFlag = false;
            string sqlStr = "select dcir_flag from step where dev_ip = @ip and cabinet_id=@gid and mid_id= @mid and chl_id = @cid";
            DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sqlStr);
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ip", ip));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", gid));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", mid));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cid", cId));
            try
            {
                using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            dcirFlag = reader.IsDBNull(0) ? true : Convert.ToBoolean(reader["dcir_flag"]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);
            }
            return dcirFlag;
        }
        /// <summary>
        /// 根据设备号，柜号，通道号查DCIR测试是单电流还是双电流
        /// </summary>
        /// <param name="devId"></param>
        /// <param name="uId"></param>
        /// <param name="cId"></param>
        /// <returns></returns>
        public static bool isDoubleCurr(string ip, int gid, int mid, int cId)
        {
            bool isDouble = true;
            int count=0;
            string sqlStr = "select count(*) from step where dev_ip = @ip and cabinet_id=@gid and mid_id= @mid and chl_id = @cid and dcir_flag = 1";
            DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sqlStr);
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ip", ip));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", gid));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", mid));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cid", cId));
            try
            {
                using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count(*)"]);
                        }
                        if (count == 2)
                        {
                            isDouble = false; 
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);
            }
            return isDouble;
        }
        /// <summary>
        /// 从历史表中查询是标准或是专业工步
        /// </summary>
        /// <param name="testId"></param>
        /// <param name="ip"></param>
        /// <param name="uId"></param>
        /// <param name="cId"></param>
        /// <returns></returns>
        public static bool GetHisStepFlagById(string testId,string ip, int gid,int mid, string cId)
        {
            bool stepFlag = false;
            string sqlStr = "select standard_flag from h_step where test_id=@testid and dev_ip = @ip and cabinet_id=@gid and mid_id= @mid and chl_id = @cid";
            DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sqlStr);
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@testid", testId));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ip", ip));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid",gid ));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", mid));
            cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cid", cId));
            try
            {
                using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            stepFlag = reader.IsDBNull(0) ? true : Convert.ToBoolean(reader["standard_flag"]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);
            }
            return stepFlag;
        }
    }
}
